/**!
 *
 * 添加首页广告。
 *
 */

$(function () {
    /** 广告表单的 jQuery 对象 */
    var $advertForm = $("#advert_form");
    /** 广告图片地址的 jQuery 对象 */
    var $homeAdUrl = $advertForm.find("input[name='homeAdUrl']");
    /** 上传广告图片的 jQuery 对象 */
    var $uploadAdvertImgBtn = $("#upload_advert_img_btn");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 添加按钮的 jQuery 对象 */
    var $addAdvertBtn = $advertForm.find("button[name='addAdvertBtn']");
    /** 广告图片的地址 */
    var homeAdUrl = "";

    $.validator.addMethod("checkHomeAdToUrl", function (value, element) {
        return this.optional(element) || urlFullRE.test(value);
    });

    // 上传图片
    var $uploadAdvertImgUploadifive = $uploadAdvertImgBtn.myUploadifive({
        multi: false,
        width: 80,
        height: 30,
        queueID: "advert_queue",
        fileType: "image/*",
        buttonText: "<i class='ace-icon iconfont icon-upload f-s-12'></i>上传图片",
        buttonClass: "btn btn-success btn-xs",
        fileObjName: "file",
        uploadScript: "{0}web/file/upload.json".format(globalBasePath),
        fileSizeLimit: String(50 * 1024),
        onUploadFn: function () {
            $uploadAdvertImgUploadifive.uploadifiveData.settings.formData.path = "/upload/home_advert";
        },
        onUploadCompleteFn: function (file, data) {
            var result = data.result;

            $homeAdUrl.val(result.filePath);
        }
    });

    $advertForm.validate({
        ignore: [],
        rules: {
            homeAdName: {
                required: true,
                maxlength: 500
            },
            homeAdUrl: {
                required: true,
                maxlength: 1000
            },
            homeAdToUrl: {
                required: true,
                maxlength: 1000,
                checkHomeAdToUrl: true
            },
            isShow : {
                required: true
            }
        },
        messages: {
            homeAdName: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入广告标题</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>广告标题的长度不能大于{0}个字符</span>"
            },
            homeAdUrl: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请上传广告图片</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>广告图片的长度不能大于{0}个字</span>"
            },
            homeAdToUrl: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入广告跳转URL</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>广告跳转URL的长度不能大于{0}个字</span>",
                checkHomeAdToUrl: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>广告跳转URL的格式不正确</span>"
            },
            isShow : {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择是否显示广告</span>"
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $addAdvertBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在提交…").attr({"disabled": "disabled"});

            return true;
        }
    });
});